/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.panel.account.registration;

import hu.glassy.core.model.Account;
import hu.glassy.core.service.AccountService;
import hu.glassy.web.component.common.CustomAjaxLink;
import hu.glassy.web.component.common.CustomAjaxSubmitLink;
import hu.glassy.web.error.ActivationTokenException;
import hu.glassy.web.page.home.PublicHomePage;
import hu.glassy.web.panel.common.CustomPanel;
import org.apache.wicket.Component;
import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * Account aktiválását lehetővé tevő panel
 *
 * @author molnar.alexander@sonrisa.hu
 */
public class ActivationPanel extends CustomPanel<Account>
{

  private static final long serialVersionUID = -3202867553245599274L;
  @SpringBean
  private AccountService accountService;
  private String activationKey;

  /**
   * Konstruktor
   *
   * @param id
   */
  public ActivationPanel(final String id, final IModel<Account> model)
  {
    super(id, model);

    final Form<Void> form = new Form<Void>("activateForm");
    add(form);
    form.add(new TextField<String>("activationKey", new PropertyModel<String>(this, "activationKey")).setRequired(true));
    form.add(generateSubmitButton());
    add(generateResendLink());
  }

  private Component generateResendLink()
  {
    return new CustomAjaxLink("resend")
    {
      private static final long serialVersionUID = 1L;

      @Override
      public void onClick(final AjaxRequestTarget target)
      {

        try
        {
          accountService.resendActivationKey(getMainObject());
          success(getString("activation.resend.success"));

        }
        catch (final ActivationTokenException e)
        {
          error(e.getMessage());
        }

      }
    };
  }

  private Component generateSubmitButton()
  {
    return new CustomAjaxSubmitLink("submit")
    {
      private static final long serialVersionUID = 1L;

      @Override
      protected void onSubmit(final AjaxRequestTarget target, final Form<?> form)
      {
        super.onSubmit(target, form);

        try
        {
          accountService.activate(getMainObject(), activationKey);
          Session.get().success(getString("activation.success"));
          setResponsePage(PublicHomePage.class);

        }
        catch (final ActivationTokenException e)
        {
          error(e.getMessage());
        }
      }
    };
  }
}
